import axios from 'axios';
import { IbusInfo } from '../type/busInfoType';

const lineId: number[] = [629, 635, 637, 638, 639, 640, 663, 664, 665];
const busInfo = async function (line: number): Promise<IbusInfo> {
  const URL = 'https://www.longyan.cn/bus_wp/timerBus.json';
  let busInfoData: IbusInfo;
  await axios
    .all(
      [0, 1].map((isUpDown) =>
        axios.get(URL, {
          params: {
            getBusInfoLiveTimeData: true,
            lineId: lineId[line - 1],
            isUpDown: isUpDown,
          },
        }),
      ),
    )
    .then(
      axios.spread(({ data: lineUp }, { data: lineDown }) => {
        busInfoData = lineUp;
        if (lineDown.retcode == '001') {
          busInfoData.data.push(...lineDown.data);
        }
      }),
    );
  return busInfoData;
};

export { busInfo };
